#include <stdio.h>
#include <stdlib.h>
#include <sys/types.h>
#include <unistd.h>
#include <sys/wait.h>
#include <errno.h>
#include <string.h>

int main()
{
    pid_t id = fork();
    if (id < 0)
    {
        printf("error: %d, errstring: %s\n", errno, strerror(errno));
        return errno;
    }
    else if (id == 0)
    {
        int cnt = 5;
        while(cnt)
        {
            printf("子进程运行中, pid: %d\n", getpid());
            cnt--;
            sleep(1);
        }
        exit(34);
    }
    else 
    {
        sleep(6);
        int status = 0;
        pid_t rid = waitpid(id, &status, 0);
        if (rid > 0)
        {
            printf("wait sub process sucess, rid: %d, status: %d, s: %d\n", rid, (status>>8)&0xFF, WEXITSTATUS(status));
           // printf("父进程运行中, pid: %d\n", getpid());
        }
        else {
            printf("waitpid");
        }
        while(true)
        {
            printf("父进程运行中, pid: %d\n", getpid());
            sleep(1);
        }
    }
    return 0;
}
